package Task_010404;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;

public class DBUtils {

    /**
     * 定义变量
     */
    private static String userName;//用户名
    private static String userPw;//密码
    private static String url;//数据库连接地址
    private static String driverClass;//驱动地址
    private static DruidDataSource dataSource = new DruidDataSource();//创建 德鲁伊连接池 连接对象

    private Connection connection;//连接对象
    private PreparedStatement preparedStatement;//预状态通道对象
    protected ResultSet resultSet;//结果集对象
    private int count;//存储受影响的行数


    /**
     * 加载驱动
     */
    static {
        //加载 属性文件 数据
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driverClass = bundle.getString("driverclass");
        url = bundle.getString("url");
        userName = bundle.getString("username");
        userPw = bundle.getString("userpw");
        //设置属性
        dataSource.setUsername(userName);
        dataSource.setPassword(userPw);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClass);
    }


    /**
     * 获得链接
     * @return
     */
    protected Connection getConnection() {
        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }


    /**
     * 获得 预状态通道 对象
     * @param sql
     * @return
     */
    protected PreparedStatement getPreparedStatement(String sql) {
        try {
            preparedStatement = getConnection().prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return preparedStatement;
    }


    /**
     * 绑定参数
     * @param list
     */
    protected void param(List list) {//List保存的是给占位符所赋的值
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                try {
                    preparedStatement.setObject(i+1, list.get(i));
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }


    /**
     * 执行操作（增删改）
     * @param sql
     * @param list
     * @return
     */
    protected void update(String sql, List list) {
        getPreparedStatement(sql);
        param(list);
        try {
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


    /**
     * 执行操作（查）
     * @param sql
     * @param list
     * @return
     */
    protected ResultSet query(String sql, List list) {
        getPreparedStatement(sql);
        param(list);
        try {
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }


    /**
     * 关闭资源
     */
    protected void close(){
        try {
            if(connection != null){
                connection.close();
            }
            if(preparedStatement != null){
                preparedStatement.close();
            }
            if(resultSet != null){
                resultSet.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
